package com.qing.springboot01.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.qing.springboot01.common.BaseContext;
import com.qing.springboot01.common.R;
import com.qing.springboot01.entity.AddressBook;
import com.qing.springboot01.mapper.AddressBookMapper;
import com.qing.springboot01.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 *
 * @author tina
 * @since 2023-06-26 04:09:20
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;



//    新增
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setCreateUser(BaseContext.getCurrentId());
        addressBook.setUpdateUser(BaseContext.getCurrentId());

        addressBook.setIsDefault(false);
        addressBook.setIsDeleted(0);
        log.info("addressBook:{}", addressBook);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }
// 修改
    @PutMapping
    public R<AddressBook> add(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setUpdateTime(LocalDateTime.now());
        log.info("addressBook:{}", addressBook);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    // 删除
    @DeleteMapping
    public R delete(Long ids) {
        if(addressBookService.deleteById(ids)>0){
            return R.success("删除成功");
        }else{
            return R.error("删除失败");
        }

    }
    //默认地址
    @PutMapping("default")
    public R<AddressBook> saveDefault(@RequestBody AddressBook addressBook){
        log.info("addressBook:{}",addressBook);
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault,false);

        addressBookService.update(wrapper);
        addressBook.setIsDefault(true);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
        }

        //id查询
    @GetMapping("/{id}")
    public R get(@PathVariable long id){
        AddressBook addressBook = addressBookService.getById(id);
        if(addressBook != null){
            return R.success(addressBook);
        }else{
            return R.error("未找到");
        }
    }

    //默认地址
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,true);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if(null == addressBook){
            return R.error("未找到");
        }else{
            return R.success(addressBook);
        }
    }

    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}",addressBook);

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getIsDefault);
        return R.success(addressBookService.list(queryWrapper));
    }
}

